/*********************************************************************************
 *      Copyright:  (C) 2022 xiezongyi<1764417007@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  cJSON_worker.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(14/05/22)
 *         Author:  xiezongyi <1764417007@qq.com>
 *      ChangeLog:  1, Release initial version on "14/05/22 20:13:39"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <string.h>
#include <cJSON.h>
#include "database.h"

#define LEN		128

//将数据打包成JSON格式
int packet_json(char *buf_json, sample_data_t *s_data)
{
	char  *out;
	cJSON *root = NULL;
	cJSON *params = NULL;

	if(!s_data)
	{
		printf("Invaild input arguments\n");
		return -1;
	}

	root = cJSON_CreateObject();
	params = cJSON_CreateObject();

	cJSON_AddNumberToObject(params, "CurrentTemperature", s_data->temp);
	cJSON_AddStringToObject(params, "time", s_data->datatime);
	cJSON_AddStringToObject(params, "SerialNo", s_data->ID);
	cJSON_AddItemToObject(root, "params", params);

	memset(buf_json, 0, sizeof(buf_json));
	strncpy(buf_json, cJSON_Print(root), LEN);

	out = cJSON_Print(root);
	printf("Pub send the message to sub:\n ");
	printf("%s\n", out);

	// 释放内存  
	cJSON_Delete(root);

	return 0;
}

